/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-15
*/
package com.rzico.article.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.article.entity.Talent;
import com.rzico.article.entity.TalentFollow;
import com.rzico.article.model.TalentFollowVo;
import com.rzico.article.service.TalentFollowService;
import com.rzico.article.service.TalentService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 我关注达人控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-15
 */
@Api(description = "我关注达人接口")
@RestController
@RequestMapping("/member/talentFollow")
public class TalentFollowMemberController extends BaseController {

    @Autowired
    private TalentFollowService talentFollowService;

    @Autowired
    private TalentService talentService;

    @Autowired
    private MemberService memberService;

    /**
     * 分页查询我关注达人
     *
     * @return
    */
    @ApiOperation("分页查询我关注达人")
    @GetMapping("/list")
    @ApiImplicitParams({    })
    public CommResult<TalentFollow> list(Date startDate, Date endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();

        Member member = memberService.getCurrent();
        if (null == member) {
            return CommResult.error("无用户信息,请先登录");
        }

        params.put("memberId", member.getId());

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<TalentFollowVo> list = talentFollowService.selectTalentFollowList(params);

        Talent tt = talentService.findByMember(member.getId());
        for (TalentFollowVo vo:list) {
            vo.setFollowFlag(false);
            if (tt!=null) {
                Map<String,Object> p = new HashMap<>();
                p.put("talentId", tt.getId());
                p.put("memberId", vo.getMemberId());
                Integer w = talentFollowService.selectRowCount(p);
                if (w > 0) {
                    vo.setFollowFlag(true);
                }
            }
        }

        PageResult<TalentFollowVo> pageResult = new PageResult<TalentFollowVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 分页查询我粉丝
     *
     * @return
     */
    @ApiOperation("分页查询我粉丝")
    @GetMapping("/fansList")
    @ApiImplicitParams({    })
    public CommResult<TalentFollow> fansList(Date startDate, Date endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();

        Member member = memberService.getCurrent();
        if (null == member) {
            return CommResult.error("无用户信息,请先登录");
        }


        Talent talent = talentService.findByMember(member.getId());
        if (talent!=null) {
            params.put("talentId", talent.getId());
        } else {
            params.put("talentId", 0);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<TalentFollowVo> list = talentFollowService.selectTalentFansList(params);
        for (TalentFollowVo vo:list) {
            vo.setFollowFlag(false);
            Talent tt = talentService.findByMember(vo.getMemberId());
            if (tt!=null) {
                Map<String,Object> p = new HashMap<>();
                p.put("talentId", tt.getId());
                p.put("memberId", member.getId());
                Integer w = talentFollowService.selectRowCount(p);
                if (w > 0) {
                    vo.setFollowFlag(true);
                }
            }
        }
        PageResult<TalentFollowVo> pageResult = new PageResult<TalentFollowVo>(list, startPage.getTotal(), pageable);

        //对查出的list进行处理
        //talentFollowService.followDispose(params,member,pageResult.getData());
        return CommResult.success(pageResult);
    }

    /**
     * 保存我关注达人
     *
     * @param talentFollow
     * @return
     */
    @ApiOperation("保存我关注达人")
    @PostMapping("/save")
    public CommResult<TalentFollow> save(@RequestBody TalentFollow talentFollow) {

        Member member = memberService.getCurrent();
        if (null == member) {
            return CommResult.error("无用户信息,请先登录");
        }
        boolean followed = talentFollowService.isFollowed(String.valueOf(talentFollow.getId()));
        if (true) {
            return CommResult.error("请勿关注自己");
        }
        if (member.getId().equals(talentFollow.getId()))
        talentFollow.setCreateDate(new Date());
        talentFollow.setModifyDate(new Date());
        talentFollow.setMemberId(member.getId());
        TalentFollow result = talentFollowService.addLove(member, talentFollow);
        return CommResult.success(result);

    }


    /**
     * 批量删除我关注达人
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除我关注达人", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除我关注达人,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<TalentFollow> del(@PathVariable String ids) {

        Member member = memberService.getCurrent();
        if (null == member) {
            return CommResult.error("无用户信息,请先登录");
        }

        TalentFollow talentFollow = talentFollowService.selectByPrimaryKey(ids);
        talentFollowService.decLove(talentFollow);
        return CommResult.success();
    }
}
